#include<windows.h>
#include "Engine.h"

//Application entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreviousInstance, LPSTR lpcmdline, int nCmdShow)
{
	WNDCLASSEX wnd_Structure;
	MSG msg_Message;

	//Constructor is called which loads config
	Engine eng;
	
	//These commands setup the window
	wnd_Structure.cbSize = sizeof(WNDCLASSEX);
	wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
	wnd_Structure.lpfnWndProc = DefWindowProc;
	wnd_Structure.cbClsExtra = 0;
	wnd_Structure.cbWndExtra = 0;
	wnd_Structure.hInstance = GetModuleHandle(NULL);
	wnd_Structure.hIcon = NULL;
	wnd_Structure.hCursor = NULL;
	wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
	wnd_Structure.lpszMenuName = NULL;
	wnd_Structure.lpszClassName = L"WindowClassName";
	wnd_Structure.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	RegisterClassEx(&wnd_Structure);

	//Create the window
	HWND hWnd = CreateWindowEx(WS_EX_CONTROLPARENT, L"WindowClassName", L"Kumea Renderer", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
		100, 100, eng.mDatabase.GetValue("width"), eng.mDatabase.GetValue("height"), 
		NULL, NULL, GetModuleHandle(NULL), NULL);

	//Start the engine
	eng.Initialize(hWnd);

	//Lets the windows messages work until the engine requests to quit
	for (;;)
	{
		if (PeekMessage(&msg_Message, hWnd, 0, 0, PM_REMOVE))
		{
			DispatchMessage(&msg_Message);
		}
		if (eng.QuitRequested())
		{
			break;
		}
	}

	//Stop the engine
	eng.Destroy();

	//Destroy the window
	DestroyWindow(hWnd);

	return 0;
}